<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@taglib prefix="s" uri="/struts-tags"%>
<%@ page
	import="com.onlineMIS.common.Common_util,com.onlineMIS.ORM.entity.headQ.finance.FinanceCategory,com.onlineMIS.ORM.entity.headQ.supplier.finance.FinanceBillSupplier"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>千禧连锁店管理信息系统</title>
<%@ include file="../../../common/Style.jsp"%>
<script>
var itemSize = <s:property value="formBean.order.financeBillItemList.size"/>;
//付款单
var paidBillType = <%=FinanceBillSupplier.FINANCE_PAID_HQ%>;
//收款单
var incomeBillType = <%=FinanceBillSupplier.FINANCE_INCOME_HQ%>;
var increaseBillType = <%=FinanceBillSupplier.FINANCE_INCREASE_HQ%>;//应付
var decreaseBillType = <%=FinanceBillSupplier.FINANCE_DECREASE_HQ%>;//应付减少
//供应商预付款
var prepaidBillType = <%=FinanceBillSupplier.FINANCE_PREPAID_HQ%>;

//应付增加减少(旧：应付应收未拆分)					
//  INCREASE_DECREASE_ACCT_TYPE
//应付账户
var increaseDecreaseAcctType =  <%=FinanceCategory.INCREASE_DECREASE_ACCT_SUPPLIER_TYPE%>;

//客户预收款
var prepaidCustomerAcctType =  <%=FinanceCategory.PREPAY_ACCT_TYPE%>;
//供应商预付款
var prepaidSupplierAcctType =  <%=FinanceCategory.PREPAY_ACCT_SUPPLIER_TYPE%>;

$(document).ready(function(){
	parent.$.messager.progress('close'); 
	loadResult();
	for (var i = 0; i < itemSize; i++){
        $("#financeBillItem" + i).numberbox({
           onChange: function (newValue,oldValue) {
        	   recalcualteTotal();
          }
        });
	}
    
});


function saveToDraft(){
	$("#draftBtn").linkbutton("disable");
	if (validateForm()){
		var params = $("#financeBillForm").serialize(); 
	    $.post("<%=request.getContextPath()%>/action/financeSupplierJSON!saveFBToDraft",params, financeBillBK,"json");	
	}else{
		$("#draftBtn").linkbutton("enable");
	}
}
function postBill(){
	//防止重复提交表单
	$("#postBtn").linkbutton("disable");
	if (validateForm()){
		$.messager.confirm('单据过账确认', "你确定要单据过账?", function(r){
			if (r){
				var params = $("#financeBillForm").serialize(); 
	    		$.post("<%=request.getContextPath()%>/action/financeSupplierJSON!postFB",params, financeBillBK,"json");	
			}else{
				$("#postBtn").linkbutton("enable");
			}
		});
	}else{
		$("#postBtn").linkbutton("enable");
	}
}
function financeBillBK(data){
	var response = data.response;
	var returnCode = response.returnCode;
	if (returnCode != SUCCESS)
		$.messager.alert('错误', response.message, 'error');
	else{
		$.messager.alert({
			icon: 'info',
			title: '保存成功',
			msg: response.message,
			fn: function(){
				window.location.href = 'financeSupplierJSP!preCreate';
			}
		});
	}
}
function deleteBill(){

	$.messager.confirm('删除单据确认', "你确定要删除单据", function(r){
		if (r){
			   document.financeBillForm.action = "<%=request.getContextPath()%>/action/financeSupplierJSP!deleteFB";
			   document.financeBillForm.submit();
		}
	});
}
function cancelBill(){
	$.messager.confirm('红冲单据确认', "你确定要红冲单据", function(r){
		if (r){
			   document.financeBillForm.action = "<%=request.getContextPath()%>/action/financeSupplierJSP!cancelFB";
			   document.financeBillForm.submit();
		}
	});	
}
function recalcualteTotal(){
	var invoiceTotalInput = $("#invoiceTotal");
	var total =0;
	for (var i = 0; i < itemSize; i++){
	    var itemTotalS = $("#financeBillItem" + i).numberbox('getValue');
	    var itemTotal ;
	    if (itemTotalS == "")
		    itemTotal = 0;
	    else 
	    	itemTotal = parseFloat(itemTotalS);
		total += itemTotal;
	}
	invoiceTotalInput.numberbox('setValue',(total).toFixed(2));
}

/**
 * 验证逻辑
   1. 预付款单 - 不能选择预付/预收款账户，应收/应付账户
   2. 应收应付单 - 只能选择应收应付账户
   3. 付款/收款单 - 只能选择自建账目和供应商预付款
 */
function validateForm(){
	var charInNum = false;
	var error = "";
	
	if (!$('#financeBillForm').form('validate'))
		return;

	var invoiceTotal = $("#invoiceTotal").val();
	if (parseFloat(invoiceTotal) == 0) 
		error += "单据没有输入金额项,请检查<br\>";

	var billType = $("#financeBillType").val();
	var financeBillSupplier = $("#supplierId").val();

	if (billType == 0){
		error += "单据种类 - 不能为空<br\>";
	}

	if (financeBillSupplier == 0){
		error += "供应商  - 不能为空<br\>";
	}	

	//2. 如果是应付增加减少单据
	if (billType == increaseBillType || billType == decreaseBillType){
		for (var i = 0; i < itemSize; i++){
		    var acctType = $("#acctType" + i).val();

		    if (acctType != increaseDecreaseAcctType && $("#financeBillItem"+i).val() !=0){
		    	error += "应收增加/减少单据  - 只能选择【应收账户】<br\>";
			}  
		}
	}

	//3. 如果是付款收款单据
	if (billType == incomeBillType || billType == paidBillType){
		for (var i = 0; i < itemSize; i++){
		    var acctType = $("#acctType" + i).val();

		    if (acctType == increaseDecreaseAcctType && $("#financeBillItem"+i).val() !=0){
		    	error += "付款单/收款单  - 不能选择'应付增加/减少'账户<br\>";
			}  
		    
		    if (acctType == prepaidCustomerAcctType && $("#financeBillItem"+i).val() !=0){
		    	error += "付款单/收款单  - 不能选择'客户预收款'账户<br\>";
			}  		    
		}
	}	
	
	//4. 如果是供应商预付款
	if (billType == prepaidBillType){
		for (var i = 0; i < itemSize; i++){
		    var acctType = $("#acctType" + i).val();

		    if (acctType == increaseDecreaseAcctType && $("#financeBillItem"+i).val() !=0){
		    	error += "供应商预付款  - 不能选择'应付增加/减少'账户<br\>";
			}  
		    
		    if (acctType == prepaidCustomerAcctType && $("#financeBillItem"+i).val() !=0){
		    	error += "供应商预付款  - 不能选择'客户预收款'账户<br\>";
			}  		
		    
		    if (acctType == prepaidSupplierAcctType && $("#financeBillItem"+i).val() !=0){
		    	error += "供应商预付款  - 不能选择'供应商预付款'账户<br\>";
			}  	
		}
	}	

	if (error != ""){
		$.messager.alert('验证错误', error, 'error');
       return false;
	} else
		return true;
}

function getSupplierFinance(supplierId){
	//var chainId = $("#financeBillChainStore").val();
	if (supplierId != 0){
	    var params = "formBean.order.supplier.id=" + supplierId; 
	    $.post("financeSupplierJSON!getSupplierAcctFinance",params, getCurrFinanceBackProcess,"json");
	} else 
		$("#currentFinance").html("");
}

function getCurrFinanceBackProcess(data){
	var response = data.response;
	if (response.returnCode != SUCCESS){
		$("#currentFinance").html("");
		$.messager.alert('错误', response.message, 'error');
	} else {
		var dataMap = response.returnValue
		$("#currentFinance").html("当前待付款 : " + dataMap.cf);
	}
		
}

function changeSelect(value){
	//window.location.href = "financeSupplierJSP!preCreate?formBean.order.type="+value;
	
	var supplierId = $("#supplierId").val();
	if(supplierId == 0){
		$.messager.alert('提示', '请选择供应商信息', 'warning');
	}else{
		window.location.href = "financeSupplierJSP!preCreate?formBean.order.type="+value +"&formBean.order.supplier.id="+supplierId;
	}	
	
}

function chooseSupplier(supplierId){
	getSupplierFinance(supplierId);
}


function changeSelectBySupplierInput(supplierId){
	var value = $("#financeBillType").combobox("getValue");
	window.location.href = "financeSupplierJSP!preCreate?formBean.order.type="+value +"&formBean.order.supplier.id="+supplierId;
}

function changeSelectByCustInputClear(){
	window.location.href = "financeSupplierJSP!preCreate";
}
</script>
</head>
<body>

	<s:form action="/action/financeHQJSP!saveToDraft" method="POST"
		name="financeBillForm" id="financeBillForm" theme="simple">
	<%@ include file="../../common/JRComponent.jsp"%>
		<table width="80%" align="center" class="OuterTable">
			<tr>
				<td>
					<table width="100%" border="0">
						<tr>
							<td colspan="7">
								<div class="errorAndmes">
									<s:actionerror cssStyle="color:red" />
									<s:actionmessage cssStyle="color:blue" />
								</div>
								<table width="100%" align="left" border="0" id="org_table">
									<tr class="PBAOuterTableTitale">
										<td height="50" colspan="3">供应商财务单据 <s:property
												value="formBean.order.statusS" /><br /> -
											总部管理人员通过此功能创建/修改/删除供应商之间的往来财务单据 <s:hidden
												name="formBean.order.id" />
										</td>
									</tr>
									<tr class="InnerTableContent">
										<td width="280" height="35">单据种类 ： <s:select
												cssClass="easyui-combobox" style="width:150px;"
												 id="financeBillType"
												name="formBean.order.type" list="formBean.order.typeHQMap"
												data-options="editable:false,onChange:function(param1, param2){changeSelect(param1);}" 
												listKey="key" listValue="value" /></td>

										<td>单据日期 ： <s:textfield id="billDate"
												name="formBean.order.billDate" cssClass="easyui-datebox"
												data-options="width:100,editable:false" />
										</td>
										<td width="400"><%@ include
												file="../SupplierInput.jsp"%>
											<div style="display: block;" id="currentFinance"></div>
										</td>
									</tr>
									
								</table>
							</td>
						</tr>

						<tr class="InnerTableContent">
							<td height="4" colspan="7"><hr width="100%" color="#FFCC00" /></td>
						</tr>
						<tr>
							<td><table width="100%" align="left" class="OuterTable"
									id="org_table">
									<tr class="PBAInnerTableTitale" align='left'>
										<th width="60" height="35">序号</th>
										<th width="110">账户名称</th>
										<th width="120">金额</th>
										<th width="199">说明</th>
									</tr>
									<s:iterator value="formBean.order.financeBillItemList"
										status="st" id="billItem">
										<tr class="InnerTableContent"
											<s:if test="#st.even"> style='<%=Common_util.EVEN_ROW_BG_STYLE %>'</s:if>>
											<td height="25"><s:property value="#st.index + 1" /></td>
											<td><s:property
													value="#billItem.financeCategory.itemName" /> 
												<input type="hidden" id="acctType<s:property value="#st.index"/>"
												name="formBean.order.financeBillItemList[<s:property value="#st.index"/>].financeCategory.type"
												value="<s:property value="#billItem.financeCategory.type"/>" />
												
												<input type="hidden"
												name="formBean.order.financeBillItemList[<s:property value="#st.index"/>].id"
												value="<s:property value="#billItem.id"/>" /> 
												
												<input
												type="hidden"
												name="formBean.order.financeBillItemList[<s:property value="#st.index"/>].financeCategory.id"
												value="<s:property value="#billItem.financeCategory.id"/>" />
											</td>
											<td><input type="text"
												id="financeBillItem<s:property value="#st.index"/>"
												name="formBean.order.financeBillItemList[<s:property value="#st.index"/>].total"
												value="<s:property value="#billItem.total"/>" size="6"
												class="easyui-numberbox" value="0"
												data-options="min:0,max:10000000,precision:2"
												onchange="recalcualteTotal();" /></td>
											<td><input type="text"
												name="formBean.order.financeBillItemList[<s:property value="#st.index"/>].comment"
												value="<s:property value="#billItem.comment"/>" data-options="prompt:'详情说明!'"
												class="easyui-textbox"
												data-options="validType:'length[0,100]'" /></td>
										</tr>
									</s:iterator>
									<tr align='left' class="PBAInnerTableTitale">
										<td height="35" colspan="2">总计</td>
										<td><s:textfield id="invoiceTotal" cssClass="easyui-numberbox" data-options="min:0,max:20000000,precision:2"
												name="formBean.order.invoiceTotal" readonly="true" size="6" /></td>
										<td></td>
									</tr>
								</table></td>
						</tr>
						
						<s:if test="formBean.order.type == 1 || formBean.order.type == 2">
							<tr class="InnerTableContent">
								<td height="25" colspan="7">折让 ： <s:textfield
												cssClass="easyui-numberbox" data-options="min:0,max:200000,precision:2"
												name="formBean.order.invoiceDiscount" /></td>
						</tr>
						</s:if>
						
						
						<tr class="InnerTableContent">
								<td height="25" colspan="7">备注 ： <s:textfield
												cssClass="easyui-textbox" style="width:300px" data-options="prompt:'请输入单据备注信息!'"
												name="formBean.order.comment" /></td>
						</tr>			
						<tr class="InnerTableContent">
							<td height="4" colspan="7">
							
								<s:if test="#session.LOGIN_USER.containFunction('financeSupplierJSON!saveFBToDraft')  && formBean.canSaveDraft"> 
 									 <a id="draftBtn" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-ok'" onclick="saveToDraft();">保存草稿</a> 
								</s:if> 
								<s:if test="#session.LOGIN_USER.containFunction('financeSupplierJSON!postFB') && formBean.canPost">
									<a id="postBtn" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-save'" onclick="postBill();">单据过账</a>
								</s:if>
								<s:if
									test="#session.LOGIN_USER.containFunction('financeSupplierJSP!deleteFB') && formBean.canDelete">
									<a id="dtn2" href="#" class="easyui-linkbutton"
										data-options="iconCls:'icon-cancel'" onclick="deleteBill();">删除单据</a>
								</s:if>
						</tr>
					</table>
				</td>
			</tr>
			<tr class="InnerTableContent">
				<td height="4" colspan="7"><hr width="100%" color="#FFCC00" /></td>
			</tr>
		</table>
	</s:form>
</body>
</html>